$(function () {
    template.defaults.imports.dataFormat = function (value) {
        var dt = new Date(value);
        var y = dt.getFullYear()
        var m = padZero(dt.getMonth() + 1)
        var d = padZero(dt.getDate())

        var hh = padZero(dt.getHours())
        var mm = padZero(dt.getMinutes())
        var ss = padZero(dt.getSeconds())
        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    }

    function padZero(n) {
        return n < 10 ? '0' + n : n
    }
    var q = {
        pagenum: 1,
        pagesize: 2,
        cate_id: '',
        state: ''
    }
    initArticle();

    function initArticle() {
        $.ajax({
            method: 'get',
            url: '/my/article/list',
            data: q,
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message, {
                        icon: 5
                    });
                }
                var htmlStr = template('tpl-article', res);
                $('tbody').html(htmlStr);
                layui.laypage.render({
                    elem: 'pageBox',
                    count: res.total,
                    curr: q.pagenum,
                    limit: q.pagesize,
                    limits: [2, 4, 6, 8],
                    layout: ['limit', 'page', 'prev', 'next', 'count'],
                    jump: function (obj, first) {
                        //切换页码会被调用
                        //渲染页面会被调用
                        q.pagenum = obj.curr;
                        q.pagesize = obj.limit;
                        if (!first) {
                            initArticle();
                        }
                    }
                });
            }
        })
    }
    initCate()

    function initCate() {
        $.ajax({
            method: 'get',
            url: '/my/article/cates',
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg(res.message, {
                        icon: 5
                    })
                }
                var htmlStr = template('tpl-cate', res);
                $('[name=cate_id]').html(htmlStr);
                layui.form.render();
            }
        })
    }
    //完成筛选
    $('#form-search').on('submit', function (e) {
        e.preventDefault();
        q.cate_id = $('[name=cate_id]').val();
        q.state = $('[name=state]').val();
        initArticle();
    })
    $('tbody').on('click', '.btn-delete', function () {
        var btns = $('.btn-delete').length;
        var id = $(this).attr('data-id');
        layer.confirm('您确定要删除吗?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            $.ajax({
                method: 'get',
                url: '/my/article/delete/' + id,
                success: function (res) {
                    if (res.status !== 0) {
                        return layer.msg(res.message, {
                            icon: 5
                        });
                    }
                    layer.msg(res.message, {
                        icon: 6
                    })
                    if (btns === 1) {
                        
                         = q.pagenum === 1 ? q.pagenum : q.pagenum - 1;
                    }
                    initArticle();
                }
            })
            layer.close(index);
        })

    })
})